package cqrtplm.aop;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.hustcad.plm.pdm.folder.model.dto.HyperlinkDTO;
import com.hustcad.plm.pdm.folder.model.vo.SubfolderVO;
import com.hustcad.plm.pdm.folder.model.vo.SubfolderWebVO;
import com.hustcad.plm.pdm.folder.service.TyplmHyperlinkService;
import com.hustcad.plm.pdm.folder.service.TyplmSubFolderLinkService;
import com.hustcad.plm.pdm.form.model.dto.FormDataDTO;
import com.ty.basic.entity.form.FormDataDO;
import cqrtplm.entity.ObjectStorageDO;
import cqrtplm.mapper.StoragePlanningMapper;
import cqrtplm.service.ObjectStorageService;
import cqrtplm.vo.PDFProjectVO;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Arrays;

@Aspect
@Component
@Slf4j
public class CreateFormAOP {

    @Resource
    private ObjectStorageService objectStorageService;
    @Resource
    private StoragePlanningMapper storagePlanningMapper;
    @Resource
    private TyplmSubFolderLinkService typlmSubFolderLinkService;
    @Resource
    private TyplmHyperlinkService typlmHyperlinkService;

    @Around(value = "execution(* com.hustcad.plm.pdm.form.service.impl.TyFormDataExtServiceImpl.createFormDataWithFile(..))")
    public Object createFormDataWithFile(ProceedingJoinPoint point) throws Throwable {
        FormDataDTO formDataDTO= (FormDataDTO) point.getArgs()[0];
        String typeName=objectStorageService.selectTypeDisplayNameByTypeId(formDataDTO.getTypeoid());
        ObjectStorageDO objectStorageDO = objectStorageService.selectOneByTypeDefName(typeName);
        boolean isCreateHyperlinks=false;
        FormDataDTO formDataDTO2=null;
        if (formDataDTO.getContainerotype().equals("ty.inteplm.product.CTyPDMLinkProduct")){
            if (ObjectUtil.isNotEmpty(objectStorageDO)){
                PDFProjectVO v = storagePlanningMapper.selectPdmIds(objectStorageDO.getContextName());
                String folderId = null;
                String[] split = objectStorageDO.getFolderPath().split("/");
                for (int i = 0; i < split.length; i++) {
                    String id=null;
                    if (StrUtil.isBlank(split[i])) {
                        continue;
                    }
                    if (i==1){
                        id= storagePlanningMapper.selectOidByNameAndCid(v.getOId(),
                                                                        split[i], "0");
                        if (StrUtil.isNotBlank(id)) {
                            folderId=id;
                        }
                    }else {
                        id = storagePlanningMapper.selectOidByNameAndCid(v.getOId(), split[i], folderId);
                        if (StrUtil.isNotBlank(id)) {
                            folderId= storagePlanningMapper.selectOidByNameAndCid(v.getOId(),
                                                                                  split[i], folderId);
                        }

                    }
                    if (StrUtil.isBlank(id)){
                        //创建文件夹
                        SubfolderVO subfolder=new SubfolderVO();
                        subfolder.setName(split[i]);
                        subfolder.setDescription("--系统自动创建--文件夹--");
                        subfolder.setParentOid(i==1?v.getDefaultcabinetoid():folderId);
                        subfolder.setParentOtype(i==1?"ty.inteplm.folder.CTyCabinet":"ty.inteplm.folder.CTySubFolder");
                        SubfolderWebVO subfolderWebVO = typlmSubFolderLinkService.createSubfolder(subfolder);
                        folderId=subfolderWebVO.getOid();
                    }
                }
                formDataDTO.setSubfolderoid(new BigInteger(folderId));
                formDataDTO.setContaineroid(new BigInteger(v.getOId()));
            }
        }else if (formDataDTO.getContainerotype().equals("ty.inteplm.project.CTyProject")){
            String level = storagePlanningMapper.queryExtendedAttributes(formDataDTO.getContaineroid().toString(), "密级");
            if (StrUtil.isNotBlank(level)&&level.equals("非密")){
                PDFProjectVO v = storagePlanningMapper.selectPdmIds(objectStorageDO.getContextName());
                if(ObjectUtil.isNotEmpty(v)){
                    isCreateHyperlinks=true;
                    formDataDTO2=new FormDataDTO();
                    BeanUtil.copyProperties(formDataDTO, formDataDTO2);

                    formDataDTO.setContainerotype("ty.inteplm.product.CTyPDMLinkProduct");
                    formDataDTO.setContaineroid(new BigInteger(v.getOId()));
                    String folderId = null;
                    String[] split = objectStorageDO.getFolderPath().split("/");
                    for (int i = 0; i < split.length; i++) {
                        String id=null;
                        if (StrUtil.isBlank(split[i])) {
                            continue;
                        }
                        if (i==1){
                            id= storagePlanningMapper.selectOidByNameAndCid(v.getOId(),
                                                                            split[i], "0");
                            if (StrUtil.isNotBlank(id)) {
                                folderId=id;
                            }
                        }else {
                            id = storagePlanningMapper.selectOidByNameAndCid(v.getOId(), split[i], folderId);
                            if (StrUtil.isNotBlank(id)) {
                                folderId= storagePlanningMapper.selectOidByNameAndCid(v.getOId(),
                                                                                      split[i], folderId);
                            }

                        }
                        if (StrUtil.isBlank(id)){
                            //创建文件夹
                            SubfolderVO subfolder=new SubfolderVO();
                            subfolder.setName(split[i]);
                            subfolder.setDescription("--系统自动创建--文件夹--");
                            subfolder.setParentOid(i==1?v.getDefaultcabinetoid():folderId);
                            subfolder.setParentOtype(i==1?"ty.inteplm.folder.CTyCabinet":"ty.inteplm.folder.CTySubFolder");
                            SubfolderWebVO subfolderWebVO = typlmSubFolderLinkService.createSubfolder(subfolder);
                            folderId=subfolderWebVO.getOid();
                        }
                    }
                    formDataDTO.setSubfolderoid(new BigInteger(folderId));
                    formDataDTO.setContainerotype("ty.inteplm.product.CTyPDMLinkProduct");
                    formDataDTO.setContaineroid(new BigInteger(v.getOId()));
                }

            }
        }
        FormDataDO formDataDO = (FormDataDO) point.proceed();
        if (isCreateHyperlinks){
            HyperlinkDTO hyperlinkDTO = assignmentToHyperlinkDTO(formDataDTO2, formDataDO);
            typlmHyperlinkService.batchCreateHyperlink(Arrays.asList(hyperlinkDTO));
        }

        return formDataDO;
    }

    public HyperlinkDTO assignmentToHyperlinkDTO(FormDataDTO formDataDTO, FormDataDO formDataDO){
        HyperlinkDTO hyperlinkDTO=new HyperlinkDTO();
        BigInteger oId=formDataDO.getOid();

        hyperlinkDTO.setOid(oId);
        hyperlinkDTO.setOtype(formDataDO.getCheckoutinfootype());
        String cId = storagePlanningMapper.selectCabinetIdByOid2(formDataDTO.getContaineroid().toString());
        hyperlinkDTO.setCabinetoid(new BigInteger(cId));
        hyperlinkDTO.setCabinetotype("ty.inteplm.folder.CTyCabinet");
        hyperlinkDTO.setContaineroid(new BigInteger(formDataDTO.getContaineroid().toString()));
        hyperlinkDTO.setContainerotype(formDataDTO.getContainerotype());
        hyperlinkDTO.setBranchid(formDataDO.getBranchid());
//        hyperlinkDTO.setContainerName(formDataDO.getC);
        hyperlinkDTO.setName(formDataDTO.getName());
        hyperlinkDTO.setSubfolderoid(new BigInteger(formDataDTO.getSubfolderoid().toString()));
        hyperlinkDTO.setSubfolderotype("ty.inteplm.folder.CTySubFolder");
//        hyperlinkDTO.setPath();
        return hyperlinkDTO;
    }
}
